#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>

#define MAX_COL   9
typedef void fn(char p[][MAX_COL]);

void getChessFromFile(char *filename,fn func);


void getChess(char *tx,int bytes, fn func){
	char chess[MAX_COL][MAX_COL]={0};
	char *p = (char*)&chess[0];
	char c = 0;
	int k = 0;
	for(int i=0;i<bytes;i++){
		c = *(tx+i);
		if('0'<=c && '9' >= c){
			*(p+k) = c-'0';
			k++;
		}
		if(k==81){
			func(chess);
			memset(chess,0,sizeof(chess));
			p = (char*)&chess[0];
			k=0;
		}
	}
}

void getChessFromFile(char *filename,fn func){
    int bytes;
	struct stat sb;
	char *tx;
	int fd;
	if(stat(filename,&sb)== -1)
		perror("can't stat input file");
	fd = open(filename,O_RDONLY);
	if (fd < 0)
		perror("can't open input file");

	tx = (char*)malloc(sb.st_size);
	if (!tx)
		perror("can't allocate tx buffer");
	else
		printf("get file len = %ld\n",sb.st_size);
	bytes = read(fd, tx, sb.st_size);
	if (bytes != sb.st_size)
		perror("failed to read input file");
	getChess(tx,bytes,func);
	free(tx);
	close(fd);

}